﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Imaginecup.factory;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Imaginecup.util.resourceMan;
using imaginecup.objectManager;
using imaginecup.information;
using imaginecup.util;
using Imaginecup.util;
using Imaginecup.stage;
using Imaginecup.gameState.malaria.stage;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.util.vertex2D;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Imaginecup.gameState.malaria.component.entity.panel;
using Imaginecup.gameState.malaria.component.factory;
using Imaginecup.gameState.malaria.stageMan;
using Imaginecup.gameState.temp;

namespace Imaginecup.gameState.malaria
{
    public class MalariaState : GameState , IControlStageMan
    {
        private IStageMan m_StageMan;
        private StateStageManID m_CurrentStateStageID;

        private ContentManager m_Content;
        private Vector2 m_MainScreenSize;
        private static MalariaState m_Malaria = null;



        private MalariaState()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";

            m_MainScreenSize = new Vector2(800, 400);
            //제일 처음 생성되는 시점에서 initState를 호출한다.( 호출하지 않으면 ObjectManager


            Vertex2DPipeMan pipeMan = Vertex2DPipeMan.GetRenderPipeMan();

            pipeMan.SetScreenSize(new Vector2(800, 480));
            pipeMan.InitPipeMan2D();

        }

        public static MalariaState GetMalariaState()
        {
            if (m_Malaria == null)
            {
                m_Malaria = new MalariaState();
            }
            return m_Malaria;
        }


        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            //    m_TrailLayer = new TouchTrailLayer();
            //     ObjectManager objMan = ObjectManager.GetObjectManager();
            //     objMan.Init(new MalariaCompInit());
            //     m_StageMan.Init();
        }

        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)  공통사항                                */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            ResourceMan resourceMan = ResourceMan.GetResourceMan();
            resourceMan.SetTextureFrame("Malaria", m_Content.Load<Texture2D>("malaria\\mosquito") , new Vector2(96 , 96));
            resourceMan.SetTextureFrame("Children", m_Content.Load<Texture2D>("malaria\\Lizard"), new Vector2(25, 25));
            resourceMan.SetTextureFrame("Hero", m_Content.Load<Texture2D>("malaria\\hero"));
            resourceMan.SetTextureFrame("HOME", m_Content.Load<Texture2D>("malaria\\home"));

            resourceMan.SetTextureFrame("Back", m_Content.Load<Texture2D>("Recycle\\back_layer"));
            resourceMan.SetTextureFrame("UI", m_Content.Load<Texture2D>("Recycle\\background"));


            resourceMan.SetTextureFrame("Particle", m_Content.Load<Texture2D>("util\\Particle"));
            resourceMan.SetTextureFrame("ProgressBar", m_Content.Load<Texture2D>("util\\progress_bar"), new Vector2(100, 15));
            resourceMan.SetTextureFrame("Time", m_Content.Load<Texture2D>("ui\\sel_num"));
            resourceMan.SetTextureFrame("Life", m_Content.Load<Texture2D>("malaria\\life"));

            #region 패널 정보설정

            Vector2 PanelSize = new Vector2(84, 106);

            resourceMan.SetTextureFrame(PanelImageMan.GetPanelImageName(ItemID.ATTACK_HAND)
                , m_Content.Load<Texture2D>("malaria\\panel\\ItemAttackPanel"), PanelSize);
            resourceMan.SetTextureFrame(PanelImageMan.GetPanelImageName(ItemID.ITEM_ID_FROG)
                , m_Content.Load<Texture2D>("malaria\\panel\\ItemFrogPanel"), PanelSize);
            resourceMan.SetTextureFrame(PanelImageMan.GetPanelImageName(ItemID.ITEM_WEB)
                , m_Content.Load<Texture2D>("malaria\\panel\\ItemWebPanel"), PanelSize);
            resourceMan.SetTextureFrame(PanelImageMan.GetPanelImageName(ItemID.ATTACK_MOVE)
                , m_Content.Load<Texture2D>("malaria\\panel\\ItemMovePanel"), PanelSize);
            resourceMan.SetTextureFrame(PanelImageMan.GetPanelImageName(ItemID.ITEM_ID_PILL)
                , m_Content.Load<Texture2D>("malaria\\panel\\ItemPillPanel"), PanelSize);
            resourceMan.SetTextureFrame(PanelImageMan.GetPanelImageName(ItemID.ITEM_SHIELD)
                , m_Content.Load<Texture2D>("malaria\\panel\\ItemShieldPanel"), PanelSize);
            resourceMan.SetTextureFrame(PanelImageMan.GetPanelImageName(ItemID.ATTACK_SLICE)
                , m_Content.Load<Texture2D>("malaria\\panel\\ItemSprayPanel"), PanelSize);
            #endregion


         //   resourceMan.SetTextureFrame("Zzin", m_Content.Load<Texture2D>("malaria\\Butterfly"));
            resourceMan.SetTextureFrame("frog", m_Content.Load<Texture2D>("malaria\\frog"));
            resourceMan.SetTextureFrame("HAND", m_Content.Load<Texture2D>("malaria\\item\\hand"));



            resourceMan.SetTextureFrame("streak5", m_Content.Load<Texture2D>("blade\\streak5"));

            resourceMan.SetTextureFrame("HealEffect", m_Content.Load<Texture2D>("malaria\\effect\\mala_effect_pill"), new Vector2(143, 152));
            resourceMan.SetTextureFrame("WebEffect", m_Content.Load<Texture2D>("malaria\\effect\\mala_effect_web"), new Vector2(125, 123));
            resourceMan.SetTextureFrame("ShieldEffect", m_Content.Load<Texture2D>("malaria\\effect\\mala_effect_mos"), new Vector2(97, 85));
            resourceMan.SetTextureFrame("HandEffect", m_Content.Load<Texture2D>("malaria\\effect\\mala_effect_hand"));
            resourceMan.SetTextureFrame("FlySwatterEffect", m_Content.Load<Texture2D>("malaria\\effect\\mala_effect_pari") , new Vector2(127 , 108));


            resourceMan.SetTextureFrame("Menu", m_Content.Load<Texture2D>("ui\\icon_stop"));

#if WINDOWS
            m_Game.Components.Add(KeyboardInputHandler.GetInputHandler());
#endif

            SoundMan.GetSoundMan().LoadSong("MalariaBGM", "sound\\bgm\\malariaBGM");
            SoundMan.GetSoundMan().LoadEffect("Frog", "sound\\malaria\\Flog");
            SoundMan.GetSoundMan().PlayBackSound("MalariaBGM");
            m_StageMan.LoadResource();

        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            //      ObjectManager.GetObjectManager().Release();

#if WINDOWS
            m_Game.Components.Remove(KeyboardInputHandler.GetInputHandler());
#endif
            m_StageMan.Release();
            ResourceMan.GetResourceMan().Release();
            SoundMan.GetSoundMan().ReleaseSound();
            m_Content.Unload();
        }

        public override void AddStateEvent()
        {
            if (m_GameStateMan.GetTopState() != this)
            {
                this.Enabled = false;
                TouchTrailLayer.GetTouchTrailLayer().TouchAllEnd();
            }
            else
                this.Enabled = true;

            base.AddStateEvent();
        }


        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

            IStageLoop stg = m_StageMan.GetStage();

            if (m_StageMan.IsClear())
            {
                ChoiceMap choiceMap = (ChoiceMap)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_ChoiceMap);
                ChangeState(choiceMap);
                choiceMap.ClearState(m_CurrentStateStageID);
                return;
                // ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
            }

            stg.Update(gameTime);

           

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            IStageLoop stg = m_StageMan.GetStage();
            stg.Draw(gameTime, m_GraphicData.GetSpriteBatch());

            base.Draw(gameTime);

#if XBOX
            //prevMouse = MouseState;
            //MouseState = Mouse.GetState();

            //if (MouseState.X > prevMouse.X)
            //{
            //    temp.X += 3;
            //}
            //else if( MouseState.X < prevMouse.X)
            //{
            //    temp.X -= 3;
            //}
            //m_GraphicData.GetSpriteBatch().Begin();
            //m_GraphicData.GetSpriteBatch().Draw(m_Cursor,  temp , Color.White);
            //m_GraphicData.GetSpriteBatch().End();

#endif
        }


        public Vector2 GetMainScreenSize()
        {
            return m_MainScreenSize;
        }

        public void SetStageManID(StateStageManID id)
        {
            m_StageMan = (MalariaStageMan)MalariaStageManFactory.GetMalariaStageManFactory().GetObject( id );
            m_CurrentStateStageID = id;
        }
    }
}
